"""
@Author  : 康帅
@Time    : 2021/3/22 11:05
@Function: 迭代模式：医院挂号案例
"""


class Customer:
    """ 客户 """

    def __init__(self, name):
        self.__name = name
        self.__num = 0
        self.__clinics = None

    def get_name(self):
        return self.__name

    def register(self, system):
        system.push_customer(self)

    def set_num(self, num):
        self.__num = num

    def get_num(self):
        return self.__num

    def set_clinic(self, clinic):
        self.__clinic = clinic

    def get_clinic(self):
        return self.__clinic


class NumeralIterator:
    """ 迭代器 """

    def __init__(self, data):
        self.__data = data
        self.__cur_idx = -1

    def next(self):
        """ 移动至下一个元素 """
        if self.__cur_idx < len(self.__data) - 1:
            self.__cur_idx += 1
            return True
        else:
            return False

    def current(self):
        """ 获取当前的元素 """
        return self.__data[self.__cur_idx] if self.__cur_idx < len(self.__data) and self.__cur_idx >= 0 else None


class NumeralSystem:
    """ 排号系统 """

    __clinics = ("1号诊断室", "2号诊断室", "3号诊断室")

    def __init__(self, name):
        self.__customers = []
        self.__cur_num = 0
        self.__name = name

    def push_customer(self, customer):
        customer.set_num(self.__cur_num + 1)
        click = NumeralSystem.__clinics[self.__cur_num % len(NumeralSystem.__clinics)]
        customer.set_clinic(click)
        self.__cur_num += 1
        self.__customers.append(customer)
        print(f"{customer.get_name()}您好！您已在{self.__name}挂号成功，序号000{customer.get_num()}")

    def get_iterator(self):
        return NumeralIterator(self.__customers)


if __name__ == '__main__':
    numeral_system = NumeralSystem("挂号台")
    lily = Customer("Lily")
    lily.register(numeral_system)
    print(lily.get_clinic())

    pony = Customer("Pony")
    pony.register(numeral_system)

    nick = Customer("Nick")
    nick.register(numeral_system)

    tony = Customer("Tony")
    tony.register(numeral_system)
    print("*" * 40)

    iterator = numeral_system.get_iterator()
    while(iterator.next()):
        customer = iterator.current()
        print("下一位病人 %04d(%s) 请到 %s 就诊" % (customer.get_num(), customer.get_name(), customer.get_clinic()))
